package cn.tedu.dfxd.controller;

import cn.tedu.dfxd.entity.CommentsInfo;
import cn.tedu.dfxd.entity.Item;
import cn.tedu.dfxd.entity.User;
import cn.tedu.dfxd.mapper.CommentsMapper;
import cn.tedu.dfxd.mapper.ItemMapper;
import cn.tedu.dfxd.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author DBX
 * @effect
 * @date 2022/04/20/13:38
 */

@RestController
@CrossOrigin
@RequestMapping("/single")
public class SingleController {
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private UserMapper userMapper;

    //插入评论
    @PostMapping("/insertCom")
    public void insertCom(@RequestBody CommentsInfo commentsInfo){

       Item item =  itemMapper.recommendDetails(commentsInfo.getItemcatId());
       commentsInfo.setType(item.getTitle());//setType
       commentsInfo.setItemcatId(item.getItemCatId());//setItemcatId

        User user = userMapper.check(commentsInfo.getUserName());
        commentsInfo.setUserId(String.valueOf(user.getId()));//setUserId
        commentsInfo.setLikeNum(0);//setLikeNum

        commentsMapper.insertCom(commentsInfo);
        return ;
    }
    @GetMapping("/findCommentsList/{id}")
    public List<CommentsInfo> findCommentsList(@PathVariable Integer id){
        Item item = itemMapper.recommendDetails(id);
        return commentsMapper.findCommentsList(item.getTitle());
    }

    @GetMapping("/findCate/{id}")
    public List<Item> findCate(@PathVariable Integer id){
        Item item = itemMapper.recommendDetails(id);

        return itemMapper.findItemCatList(item.getItemCatId());

    }
}
